﻿<?php
    $con = mysql_connect('us-cdbr-iron-east-04.cleardb.net:3306','b04628a9fcb6e2','02a11cd8'); //修改数据库地址,账号密码
    if (!$con)
    {
        die('Could not connect: ' . mysql_error());
    }
    
    mysql_select_db("ad_3922392ca673e58", $con); //修改数据库名
    
    if (!function_exists('getallheaders')) 
    { 
        function getallheaders() 
        { 
               $headers = ''; 
           foreach ($_SERVER as $name => $value) 
           { 
               if (substr($name, 0, 5) == 'HTTP_') 
               { 
                   $headers[str_replace(' ', '-', ucwords(strtolower(str_replace('_', ' ', substr($name, 5)))))] = $value; 
               } 
           } 
           return $headers; 
        } 
    } 

    $var = file_get_contents('php://input');
	$tmp_str = urldecode($var);
    $res = "<div class=\'hooks\'> $tmp_str </div><br />";
    foreach (getallheaders() as $name => $value) {
    	$res.="<br />$name: $value <br />";
    } 
/*
    $var = $_POST['web_project'];
    
    $pro = json_decode($var);
    
    $res = "项目名：".$pro->name."，Commit信息：".$pro->version."，项目路径：".$pro->url;
*/
    $sql = "INSERT INTO json (content,created_time) VALUES ('$res',now())";
    
    if(!mysql_query($sql,$con)){
        echo "error: mysql connect failed：".mysql_error();
    } else {
        echo "success！";
    }

    
    function checkstr($str){ 
        $needle = "hook={";//判断是否包含这个字符 
        $tmparray = explode($needle,$str); 
        if(count($tmparray)>1){ 
            return true; 
        }else{ 
            return false; 
        } 
    }

    function prettyPrint( $json )
    {
        $result = '';
        $level = 0;
        $prev_char = '';
        $in_quotes = false;
        $ends_line_level = NULL;
        $json_length = strlen( $json );
    
        for( $i = 0; $i < $json_length; $i++ ) {
            $char = $json[$i];
            $new_line_level = NULL;
            $post = "";
            if( $ends_line_level !== NULL ) {
                $new_line_level = $ends_line_level;
                $ends_line_level = NULL;
            }
            if( $char === '"' && $prev_char != '\\' ) {
                $in_quotes = !$in_quotes;
            } else if( ! $in_quotes ) {
                switch( $char ) {
                    case '}': case ']':
                        $level--;
                        $ends_line_level = NULL;
                        $new_line_level = $level;
                        break;
    
                    case '{': case '[':
                        $level++;
                    case ',':
                        $ends_line_level = $level;
                        break;
    
                    case ':':
                        $post = " ";
                        break;
    
                    case " ": case "\t": case "\n": case "\r":
                        $char = "";
                        $ends_line_level = $new_line_level;
                        $new_line_level = NULL;
                        break;
                }
            }
            if( $new_line_level !== NULL ) {
                $result .= "\n".str_repeat( "    ", $new_line_level );
            }
            $result .= $char.$post;
            $prev_char = $char;
        }
    
        return $result;
    }
    mysql_close($con);
?>
